/*
 * web_body_visualizer.c
 * This file is part of gURL
 *
 * Copyright (C) 2014 - Aleksey Konovalov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Library General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "web_body_visualizer.h"

#ifdef HAVE_GTKHTML

#include "body_visualizer.h"
#include <gtkhtml/gtkhtml.h>

struct _WebBodyVisualizer {
    GtkScrolledWindow parent;
    GtkWidget* html;
};

struct _WebBodyVisualizerClass {
    GtkScrolledWindowClass parent;
};

static void web_body_visualizer_interface_init(BodyVisualizerInterface* iface);

G_DEFINE_TYPE_WITH_CODE(WebBodyVisualizer, web_body_visualizer, GTK_TYPE_SCROLLED_WINDOW,
                        G_IMPLEMENT_INTERFACE(BODY_VISUALIZER_TYPE,
                                              web_body_visualizer_interface_init))

static void web_body_visualizer_change_session(BodyVisualizer* self,
                                                Session* session) {
    WebBodyVisualizer* wbv = WEB_BODY_VISUALIZER(self);
    const GString* body = session ? session_get_response_body(session) : NULL;
    if(body && body->len > 0)
        gtk_html_load_from_string(GTK_HTML(wbv->html), body->str, body->len);
    else
        gtk_html_load_empty(GTK_HTML(wbv->html));
}

static void web_body_visualizer_interface_init(BodyVisualizerInterface* iface) {
    iface->change_session = web_body_visualizer_change_session;
    iface->name = "WEB";
}

static void web_body_visualizer_init(WebBodyVisualizer* self) {
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(self), GTK_SHADOW_IN);
    gtk_scrolled_window_set_hadjustment(GTK_SCROLLED_WINDOW(self), NULL);
    gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(self), NULL);
    g_object_set(G_OBJECT(self), "margin", 7, NULL);
    self->html = gtk_html_new();
    gtk_widget_show(self->html);
    gtk_container_add(GTK_CONTAINER(self), self->html);
}

static void web_body_visualizer_class_init(WebBodyVisualizerClass* klass) {}

GtkWidget* web_body_visualizer_new() {
    return g_object_new(WEB_BODY_VISUALIZER_TYPE, NULL);
}

#endif /* HAVE_GTKHTML */
